package com.haiyou.common.util.math;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Nonnull;

import org.springframework.util.ObjectUtils;

import com.google.common.collect.Lists;
import com.haiyou.common.constants.ErrCode;
import com.haiyou.common.util.check.Assert;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;

/**
 * @Description: 长类型分段 工具类
 * @author xingyuan
 * @date 2023年2月6日 下午4:24:06
 */
@Slf4j
@Getter
@Setter
@ToString
public class LongSegmentUtils {
	
	
	public static void main(String[] args) {
		LongSegmentBean bean = new LongSegmentBean();
		

		LongSegmentUtils.add(bean, 100, 1);
		LongSegmentUtils.add(bean, 100, 0);
		LongSegmentUtils.add(bean, 100, 3);

		//LongSegmentUtils.add(bean, 100, 1);
		for (LongSegment segment : bean.getSegments()) {
			log.info("start:{},end:{},type:{}",segment.getStart(),segment.getEnd(),segment.getType());
		}
		
		log.info(String.valueOf(bean.getNum()));

		log.info("=========减少============");
		
		//LongSegmentUtils.subtract(bean, 100);
		
		LongSegmentUtils.subtract(bean, 100);
		
		for (LongSegment segment : bean.getSegments()) {
			log.info("start:{},end:{},type:{}",segment.getStart(),segment.getEnd(),segment.getType());
		}
		
		log.info(String.valueOf(bean.getNum()));

	}
	
	
	
	/**
	 * 增加
	 * @param bean
	 * @param num
	 * @param type
	 */
	public static LongSegmentBean add(LongSegmentBean bean,long num,int type) {
		
		if(num <= 0) {
			return bean;
		}
		
		Assert.isTrue(bean != null, ErrCode.ERROR_PARAMETER);
		
		List<LongSegment> segments = bean.getSegments();
		
		LongSegment segment = null;
		
		if(segments.size() > 0) {
			segment = segments.get(segments.size() - 1);
		}
		
		if(segment != null && segment.getType() == type) {
			segment.setEnd(segment.getEnd() + num);
		}else if(segment != null && segment.getType() != type){
			LongSegment segment1 = new LongSegment();
			segment1.setStart(segment.getEnd() + 1);
			segment1.setEnd(segment.getEnd() + num );
			segment1.setType(type);
			segments.add(segment1);
		}else if(segment == null) {
			LongSegment segment1 = new LongSegment();
			segment1.setStart(1);
			segment1.setEnd(num);
			segment1.setType(type);
			segments.add(segment1);
		}
		
		return bean;
	}
	
	
	/**
	 * 减少
	 * @param bean
	 * @param num
	 * @return
	 */
	public static LongSegmentBean subtract(LongSegmentBean bean,long num) {
		
		if(num <= 0) {
			return bean;
		}
		
		Assert.isTrue(bean != null, ErrCode.ERROR_PARAMETER);
		
		List<LongSegment> segments = bean.getSegments();
		
		LongSegment segment = null;
		
		if(segments.size() > 0) {
			segment = segments.get(segments.size() - 1);
		}
		
		if(segment == null) {
			segment = new LongSegment();
			segment.setStart(0);
			segment.setEnd(0);
			segment.setType(0);
			segments.add(segment);
		}
				
		long value = num;
		
		List<LongSegment> removes = Lists.newArrayList();
		
		int size = segments.size();
		
		for (int i = 0; i < size; i++) {
			LongSegment longSegment1 = segments.get(i);
			
			//long start = longSegment1.getStart();
			long end = longSegment1.getEnd();
			
			value = value - end;
			
			//减出负数
			if(value < 0) {
				longSegment1.setEnd(-value);
				longSegment1.setStart(1);
				break;
			}else if(value >= 0){
				removes.add(longSegment1);
			}
		
		}
		
		if(!removes.isEmpty()) {
			segments.removeAll(removes);
		}
		
		if(segments.isEmpty()) {
			LongSegment segment1 = new LongSegment();
			segment1.setStart(-value);
			segment1.setEnd(0);
			segment1.setType(0);
			segments.add(segment1);
		}
		
		
		return bean;
	}
	
}
